package com.wuyou.designPatterns.chainofresponsibility;

/**
 * Support
 *
 * @Date 2024/10/12 14:01
 * @Author 无忧
 */
public abstract class Support {


    private String name;

    private Support next;

    public Support(String name) {
        this.name = name;
    }

    public Support setNext(Support next){
        this.next = next;
        return next;
    }

    /**
     * 解决问题的步骤
     * @param trouble
     */
    public final void support(Trouble trouble){
        if (resolve(trouble)) {
            done(trouble);
        }else if(next != null){
            next.support(trouble);
        }else {
            fail(trouble);
        }
    }

    protected abstract boolean resolve(Trouble trouble); // 解决问题的方法

    /**
     * 解决
     * @param trouble
     */
    protected void done(Trouble trouble){
        System.out.println(trouble + " is resolved by " + this + ".");
    }

    /**
     * 未解决
     * @param trouble
     */
    protected void fail(Trouble trouble){
        System.out.println(trouble + " cannot be resolved.");
    }
}
